<?php

class UserController extends Zend_Controller_Action
{

    public function init()
    {
    	
    }

    public function indexAction()
    {
    	$this->view->title = 'Users List';
        $usersDbTable = new Application_Model_DbTable_Users();
        
        $id = $this->_getCurrentUserId();
        
        $this->view->users = $usersDbTable->fetchAll('id <> ' . $id);
    }

    protected function _getCurrentUserId()
    {
    	$auth = Zend_Auth::getInstance();
    	if ($auth->hasIdentity()) {
    		return $auth->getIdentity()->id;
    	} else {
    		return 0;
    	}
    }

    public function addAction()
    {
        $form = new Application_Form_AddUser();
        
        $this->view->title = 'Add new user';
        $this->view->form = $form;
        $this->view->formResponse = '';
        
        if ($this->getRequest()->isPost()) {
        	$formData = $this->getRequest()->getPost();
        	
        	if (isset($_POST['add_user'])) {
        		if ($form->isValid($formData)) {
        			$username = $form->getValue('username');
        			$password = $form->getValue('password');
        			$email = $form->getValue('email_address');
        			$role = $form->getValue('role');
        			$firstName = $form->getValue('first_name');
        			$lastName = $form->getValue('last_name');
        		
        			$usersDbTable = new Application_Model_DbTable_Users();
        			$id = $usersDbTable->addUser($username, $password, $email, $role, $firstName, $lastName);
        			
        			if ($id != 0) {
        				$user = $usersDbTable->getUser($id);
        				$user['password'] = $password;
        				$emailUtil = new SubfolderAccessManager_Util_Email();
        				$emailUtil->sendNewAccountEmail($user);
        			}
        			
        			$this->_helper->redirector('index');
        		} else {
        			$this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
        			$form->populate($formData);
        		}
        	} else {
        		$this->_helper->redirector('index');
        	}
        	
        }
    }

    public function editAction()
    {
    	$form = new Application_Form_EditUser();
    	
    	$this->view->title = 'Edit user';
    	$this->view->form = $form;
    	$this->view->formResponse = '';
    	
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    		
    		if (isset($_POST['save_user'])) {
	    		if ($form->isValid($formData)) {
	    			
		    			$id = (int) $form->getValue('id');
		    			$emailAddress = $form->getValue('email_address');
		        		$firstName = $form->getValue('first_name');
		        		$lastName = $form->getValue('last_name');
		        		$role = $form->getValue('role');
		        		
		        		$usersDbTable = new Application_Model_DbTable_Users();
		        		$rowsUpdated = $usersDbTable->updateUser($id, $emailAddress, $firstName, $lastName, $role);
		        		
		        		if ($rowsUpdated > 0) {
		        			$user = $usersDbTable->getUser($id);
		        			$emailUtil = new SubfolderAccessManager_Util_Email();
		        			$emailUtil->sendUpdatedAccountEmail($user);
		        		}
		        		
		        		$this->_helper->redirector('index');
	    		} else {
	    			$this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
	    			$form->populate($formData);
	    		}
    		} else {
    			$this->_helper->redirector('index');
    		}
    	} else {
    		$id = $this->_getParam('id', 0);
    		if ($id > 0) {
    			$usersDbTable = new Application_Model_DbTable_Users();
    			$form->populate($usersDbTable->getUser($id));
    		}
    	}
    		
    }

    public function deleteAction()
    {
    	$this->view->title = 'Delete user';
    	if ($this->getRequest()->isPost()) {
    		$del = $this->getRequest()->getPost('del');
    		if ($del == 'Yes') {
    			$id = $this->getRequest()->getPost('id');
    			$albums = new Application_Model_DbTable_Users();
    			$albums->deleteUser($id);
    		}
    		$this->_helper->redirector('index');
    	} else {
    		$id = $this->_getParam('id', 0);
    		$albums = new Application_Model_DbTable_Users();
    		$this->view->user = $albums->getUser($id);
    	}
    }

    public function changeMailAction()
    {
        $form = new Application_Form_ChangeMail();
    	
    	$this->view->title = 'Change email address';
    	$this->view->form = $form;
    	$this->view->formResponse = '';
    	
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    	
    		if (isset($_POST['save'])) {
    			if ($form->isValid($formData)) {
    				
    				$currentEmail = $form->getValue('currentEmail');
    				$newEmail = $form->getValue('newEmail');
    				
    				$usersDbTable = new Application_Model_DbTable_Users();
    				$usersDbTable->changeUserEmail($currentEmail, $newEmail);
    				
    				$emailUtil = new SubfolderAccessManager_Util_Email();
    				$emailUtil->sendEmailChangedNotification($currentEmail, $newEmail);
    				
    				$this->_helper->redirector('index', 'index');
    			} else {
    				$this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
    				$form->populate($formData);
    			}
    		} else {
    			$this->_helper->redirector('index', 'index');
    		}
    	}
    }

    public function changePassAction()
    {
    	$form = new Application_Form_ChangePass();
    	
    	$this->view->title = 'Change password';
    	$this->view->form = $form;
    	$this->view->formResponse = '';
    	
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    	
    		if (isset($_POST['save'])) {
    			if ($form->isValid($formData)) {
    				
    				$userId = $this->_getCurrentUserId();
    				$newPassword = $form->getValue('newPass');
    				
    				$usersDbTable = new Application_Model_DbTable_Users();
    				$usersDbTable->changePassword($userId, $newPassword);
    				
    				$user = $usersDbTable->getUser($userId);
    				$emailUtil = new SubfolderAccessManager_Util_Email();
    				$emailUtil->sendPasswordChangedNotification($user, $newPassword);
    				
    				$this->_helper->redirector('index', 'index');
    			} else {
    				$this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
    				$form->populate($formData);
    			}
    		} else {
    			$this->_helper->redirector('index', 'index');
    		}
    	}
    }
}











